/*!
 * \file __hash_node_show.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frlog.h"
#include "frmem.h"
#include "frlist.h"
#include "frsutl.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frct_hash.h"
#include "frct_mng.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern void __hash_node_show(Frlist_node *p_st_node, char *p_outbuff, unsigned int ui_buff_size);

static void __case1(void)
{
    Frlist_node *p_st_node = NULL;
    char *p_outbuff = NULL;
    unsigned int ui_buff_size = 1024;

    p_outbuff = (char *)frmalloc(ui_buff_size);
    memset(p_outbuff,'\0',ui_buff_size);

    __hash_node_show(p_st_node, p_outbuff, ui_buff_size);

    CU_ASSERT_TRUE(memcmp(p_outbuff,"\0",1) == 0);

    frfree(p_outbuff);

}

static void __case2(void)
{
    Frlist_node *p_st_node = NULL;
    char *p_outbuff = NULL;
    char *p_data = NULL;
    unsigned int ui_buff_size = 1024;

    p_st_node = frlist_node_create(p_data);
    if(!p_st_node)
        return;

    p_outbuff = (char *)frmalloc(ui_buff_size);
    memset(p_outbuff,'\0',ui_buff_size);
    if(!p_outbuff)
    {
        frfree(p_st_node);
        return;
    }

    __hash_node_show(p_st_node, p_outbuff, ui_buff_size);

    CU_ASSERT_TRUE(memcmp(p_outbuff,"\0",1) == 0);

    frfree(p_st_node);
    frfree(p_outbuff);

}

static void __case3(void)
{
    Frlist_node *p_st_node = NULL;
    char *p_outbuff = NULL;
    unsigned int ui_buff_size = 0;
    Conntrack *p_st_ct = NULL;
    Frlist_header lst_hdr_l7_proto;
    L7_analyzer *p_st_L7_analyzer = NULL;
    L7_module *p_st_l7_module = NULL;
    L7_session      *p_st_session = NULL;
    L7_transaction  *p_st_transaction = NULL;
    char *p_tmp = NULL;

    //设置p_st_l7_module
    p_st_l7_module = (L7_module *)frmalloc(sizeof(L7_module));
    memset(p_st_l7_module, 0x00, sizeof(L7_module));
    p_st_l7_module->i_l7_proto = 1;
    p_st_l7_module->us_l4_proto = 6;
    p_st_l7_module->p_module_name = (char *)frstrdup((char *)"module_name");
    p_st_l7_module->p_version = (char *)frstrdup((char *)"version");
    //设置p_st_session
    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_username = (char *)frstrdup((char *)"username");
    p_st_session->i_status = APM_SESSION_START;

    //p_st_transaction
    p_st_transaction = (L7_transaction *)frmalloc(sizeof(L7_transaction));
    p_st_transaction->i_status = 1;

    p_tmp = (char *)frmalloc(strlen((char *)"transaction_code") + 1);
    snprintf(p_tmp,strlen((char *)"transaction_code") + 1,"%s",(char *)"transaction_code");
    p_st_transaction->p_transaction_code = p_tmp;

    p_st_transaction->p_transaction_title = NULL;

    p_tmp = (char *)frmalloc(strlen((char *)"server_name") + 1);
    snprintf(p_tmp,strlen((char *)"server_name") + 1,"%s",(char *)"server_name");
    p_st_transaction->p_server_name = p_tmp;

    //设置p_st_L7_analyzer
    p_st_L7_analyzer = (L7_analyzer *)frmalloc(sizeof(L7_analyzer));
    p_st_L7_analyzer->p_st_l7_module = p_st_l7_module;
    p_st_L7_analyzer->p_st_session = p_st_session;
    p_st_L7_analyzer->p_st_transaction = p_st_transaction;

    //
    p_st_node = frlist_node_create(p_st_L7_analyzer);
    lst_hdr_l7_proto.head = p_st_node;
    lst_hdr_l7_proto.size = 1;
    lst_hdr_l7_proto.tail = NULL;

    //设置p_st_ct
    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    p_st_ct->refcnt = 1;
    p_st_ct->src_ip = iptools_parse_ipaddr((char *)"192.168.1.1");
    p_st_ct->src_port = 80;
    p_st_ct->dst_ip = iptools_parse_ipaddr((char *)"192.168.0.1");
    p_st_ct->dst_port = 22;
    p_st_ct->ip_proto = 6;

    //设置p_st_node
    p_st_node = frlist_node_create(p_st_ct);
    if(!p_st_node)
        return;

    __hash_node_show(p_st_node, p_outbuff, ui_buff_size);

    CU_ASSERT_PTR_NULL(p_outbuff);

    frfree(p_st_l7_module->p_module_name);
    frfree(p_st_l7_module->p_version);
    frfree(p_st_l7_module);

    frfree(p_st_transaction->p_server_name);
    frfree(p_st_transaction->p_transaction_code);
    frfree(p_st_transaction);
    frfree(p_st_session->p_username);
    frfree(p_st_session);
    frfree(p_st_L7_analyzer);
    frfree(lst_hdr_l7_proto.head);
    frfree(p_st_node);
    frfree(p_outbuff);
    frfree(p_st_ct);

}

int add_suite___hash_node_show(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frct_hash __hash_node_show()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
//    CU_add_test(pSuite, "case4", __case4);

    return FR_SUCC;
}
